package com.zlx.store.user.config;


import com.zlx.store.user.interceptor.IpInterceptor;
import com.zlx.store.user.interceptor.JWTInterceptor;
import com.zlx.store.user.interceptor.SecurityInterceptor;
import com.zlx.store.user.interceptor.UserNameInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Resource
    private SecurityInterceptor securityInterceptor;
    @Resource
    private IpInterceptor ipInterceptor;
    @Resource
    private JWTInterceptor jwtInterceptor;
    @Resource
    private UserNameInterceptor userNameInterceptor;


    /**
     * 拦截器设置
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         * ip拦截器
         */
        registry.addInterceptor(ipInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns("/api/user/login", "/api/code/verifyEcode",
                        "/api/code/verifyByEP",
                        "/api/code/verify",
                        "/api/code/getCode.jpg",
                        "/api/user/logByName",
                        "/api/goods/queryById",
                        "/api/goodsDoc/search")
                .order(1);
        /**
         * jwt认证拦截器
         */
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns("/api/user/login", "/api/code/verifyEcode",
                        "/api/code/verifyByEP",
                        "/api/code/verify",
                        "/api/code/getCode.jpg",
                        "/api/user/logByName",
                        "/api/goods/queryById",
                        "/api/goodsDoc/search")
                .order(2);
        /**
         * 用户名拦截器
         */
        registry.addInterceptor(userNameInterceptor)
                .addPathPatterns("/api/user/login")
                .order(3);
        /**
         * 权限拦截器
         */
        registry.addInterceptor(securityInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns("/api/user/login", "/api/code/verifyEcode",
                        "/api/code/verifyByEP",
                        "/api/code/verify",
                        "/api/code/getCode.jpg",
                        "/api/user/logByName",
                        "/api/goods/queryById",
                        "/api/goodsDoc/search")
                .order(4);
    }
}
